<?php

/**
 * 后台 登录模块 Model
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: login.class.php 7 2011-07-12 02:04:50Z zhangwentao $
 * http://www.t-mac.org；
 */
class login extends Model
{

    private $user_table;
    private $userstype_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->user_table = '#@__users';
        $this->userstype_table = '#@__users_type';
    }

    /**
     * 登录验证
     * @param <type> $admin_name
     * @param <type> $admin_psw
     * @param <type> $yzm
     * @param <type> $remember 
     */
    public function check($admin_name, $admin_psw)
    {
        if (trim($admin_name) == "") {
            $this->redirect("请输入用户名!");
            exit();
        }
        if (trim($admin_psw) == "") {
            $this->redirect("请输入密码!");
            exit();
        }
        $sql = "SELECT a.type_purviews, b.* FROM $this->userstype_table a LEFT JOIN $this->user_table b ON a.rank = b.rank WHERE b.username = '{$admin_name}' LIMIT 0, 1";
        $info = $this->db->getRow($sql);
        if ($info) {
            if (md5(md5($admin_psw)) == $info['password']) {
                if (!empty($_SESSION['username'])) {
                    session_unregister("username");
                }
                if (!empty($_SESSION['uid'])) {
                    session_unregister('uid');
                }
                if (!empty($_SESSION['user_purviews'])) {
                    session_unregister('user_purviews');
                }
                //注册session
                $_SESSION['username'] = $info['username'];
                $_SESSION['uid'] = $info['uid'];
                $_SESSION['user_purviews'] = $info['type_purviews'];

                /*
                  //插adminlog 登录日志
                  $log_info = array(
                  'rank' => $info['rank'],
                  'id' => $info['id'],
                  'name' => $info['username'],
                  'logip' => $_SERVER["REMOTE_ADDR"],
                  'logtime' => time()
                  );

                  $rs = $this->db->autoExecute($this->adminlog_table, $log_info, 'INSERT');
                 */
                $this->redirect("成功登录，正在转向会员中心！", PHP_SELF . '?m=user');
                exit();
            } else {
                $this->redirect("Access Denied! 密码错误");
                exit();
            }
        } else {
            $this->redirect("Access Denied! 没有此用户");
            exit();
        }
    }

    public function out()
    {
        if (phpversion() < '4.3.0') {
            session_unregister('username');
            session_unregister('uid');
        } else {
            $_SESSION["username"] = "";
            $_SESSION['uid'] = "";
            $_SESSION['user_purviews'] = '';
        }
        $this->redirect("退出/注销成功!", PHP_SELF);
    }

}